package com.autoapitest.web.controller.project;

import com.autoapitest.common.core.controller.BaseController;
import com.autoapitest.common.core.domain.AjaxResult;
import com.autoapitest.common.core.domain.entity.ProjectBaseEntity;
import com.autoapitest.common.core.domain.entity.ProjectBaseUserEntity;
import com.autoapitest.common.core.page.TableDataInfo;
import com.autoapitest.system.domain.ProjectBaseUser;
import com.autoapitest.system.service.IProjectBaseService;
import com.autoapitest.system.service.IProjectBaseUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 项目管理
 *
 * @author yangsen
 */
@RestController
@RequestMapping("/project/manage")
public class ProjectController extends BaseController {

    @Autowired
    private IProjectBaseService projectBaseService;


    @Autowired
    private IProjectBaseUserService projectBaseUserService;

    @PreAuthorize("@ss.hasPermi('management:project:add')")
    @ApiOperation("新增项目")
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody ProjectBaseEntity projectBaseEntity)
    {
        //项目校验
        Boolean checkProject = projectBaseService.checkProject(projectBaseEntity, Boolean.TRUE);
        if (checkProject){
            int insertProject = projectBaseService.insertProject(projectBaseEntity);
            return AjaxResult.success(insertProject);
        }else {
            return AjaxResult.error("新增项目信息失败,请联系管理员");
        }
    }



    @ApiOperation("修改项目")
    @PostMapping("/update")
    public AjaxResult update(@Valid @RequestBody ProjectBaseEntity projectBaseEntity)
    {
        //项目校验
        Boolean checkProject = projectBaseService.checkProject(projectBaseEntity, Boolean.FALSE);
        if (checkProject){
            int updateProject = projectBaseService.updateProject(projectBaseEntity);
            return AjaxResult.success(updateProject);
        }else {
            return AjaxResult.error("修改项目信息失败,请联系管理员");
        }
    }

    @ApiOperation("项目列表")
    @GetMapping("/list")
    public TableDataInfo list(ProjectBaseEntity projectBaseEntity){
        startPage();
        List<ProjectBaseEntity> list = projectBaseService.selectProjectList(projectBaseEntity);
        return getDataTable(list);
    }


    @ApiOperation("新增项目成员")
    @PostMapping("/addusers")
    public AjaxResult addUsers(@Valid @RequestBody ProjectBaseUserEntity projectBaseUserEntity)
    {
        //项目校验
        Boolean checkProject = projectBaseUserService.checkProjectUser(projectBaseUserEntity);
        if (checkProject){
            Boolean insertProject = projectBaseUserService.insertProjectUsers(projectBaseUserEntity);
            return AjaxResult.success(insertProject);
        }else {
            return AjaxResult.error("新增成员失败,请联系管理员");
        }
    }


    @ApiOperation("项目成员列表")
    @GetMapping("/listusers")
    public TableDataInfo list(ProjectBaseUserEntity projectBaseUserEntity){
        startPage();
        List<ProjectBaseUser> list = projectBaseUserService.selectProjectUserList(projectBaseUserEntity);
        return getDataTable(list);
    }

    @ApiOperation("项目成员")
    @PostMapping("/listuserdelete")
    public AjaxResult listuserdelete(@Valid @RequestBody ProjectBaseUserEntity projectBaseUserEntity){
        Boolean aBoolean = projectBaseUserService.listuserdelete(projectBaseUserEntity);

        if (aBoolean){
            return AjaxResult.success(aBoolean);
        }else {
            return AjaxResult.error("删除成员失败,请联系管理员");
        }
    }



}
